package omit.wage.service.impl;

import omit.wage.constant.MessageConstant;
import omit.wage.exception.AccountNotFoundException;
import omit.wage.exception.PasswordErrorException;
import omit.wage.dto.AdminLoginDTO;
import omit.wage.entity.Administrator;
import omit.wage.mapper.AdministratorMapper;
import omit.wage.service.IAdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-11
 */
@Service
public class AdministratorServiceImpl implements IAdministratorService {
    @Autowired
    private AdministratorMapper administratorMapper;

    //员工登录
    public Administrator login(AdminLoginDTO adminLoginDTO){
        //接收传入的登录数据
        String account = adminLoginDTO.getUsername();
        String password = adminLoginDTO.getPassword();

        //根据用户名在数据库中查找
        Administrator administrator = administratorMapper.getByUsername(account);

        //处理异常
        if(administrator == null){
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if (!password.equals(administrator.getPasswords())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //3、返回实体对象
        return administrator;
    }
}
